package cn.lyw.discount.service.service.impl;

import cn.common.beancopy.util.BeanCopierUtil;
import cn.common.restresult.util.RestResult;
import cn.common.restresult.util.ResultEnum;
import cn.hutool.core.lang.generator.ObjectIdGenerator;
import cn.lyw.discount.mapper.mapper.UserDiscountMapper;
import cn.lyw.discount.mapper.pojo.SmallMiAdminDiscount;
import cn.lyw.discount.mapper.pojo.SmallMiUserDiscount;
import cn.lyw.discount.service.bo.AddUserDiscountBO;
import cn.lyw.discount.service.bo.EditUserDiscountBO;
import cn.lyw.discount.service.bo.GetUserDiscountByParamBO;
import cn.lyw.discount.service.domain.GetUserDiscountAllDO;
import cn.lyw.discount.service.domain.GetUserDiscountByParamDO;
import cn.lyw.discount.service.domain.GetUserDiscountByUserDO;
import cn.lyw.discount.service.service.UserDiscountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author lyw
 * @Description 用户优惠券逻辑层
 * @Date 12:04 2021/7/21
 * @Param
 * @return
 */
@Service
public class UserDiscountServiceImpl implements UserDiscountService {

    @Autowired
    private UserDiscountMapper userDiscountMapper;


    /**
     * @return
     * @Author lyw
     * @Description 查询全部优惠券
     * @Date 12:06 2021/7/21
     * @Param
     */
    @Override
    public List<GetUserDiscountAllDO> getUserDiscountAll() {
        //调用通用mapper
        List<SmallMiUserDiscount> smallMiUserDiscounts = userDiscountMapper.selectAll();
        //转换返回值
        List<GetUserDiscountAllDO> getUserDiscountAllDOS = BeanCopierUtil.copyList(smallMiUserDiscounts, GetUserDiscountAllDO.class);
        return getUserDiscountAllDOS;
    }


    /**
     * @return
     * @Author lyw
     * @Description 根据用户id查询优惠券
     * @Date 12:14 2021/7/21
     * @Param
     */
    @Override
    public GetUserDiscountByUserDO getUserDiscountByUser(String discountId) {
        //调用通用mapper
        SmallMiUserDiscount smallMiUserDiscount = userDiscountMapper.selectByPrimaryKey(discountId);
        //转换返回值
        GetUserDiscountByUserDO getUserDiscountByUserDO = BeanCopierUtil.copyObj(smallMiUserDiscount, GetUserDiscountByUserDO.class);
        return getUserDiscountByUserDO;
    }


    /**
     * @return
     * @Author lyw
     * @Description 根据用户id和状态查询优惠券
     * @Date 12:25 2021/7/21
     * @Param
     */
    @Override
    public List<GetUserDiscountByParamDO> getUserDiscountByParam(GetUserDiscountByParamBO bo) {
        //转换参数
        SmallMiUserDiscount smallMiUserDiscount = BeanCopierUtil.copyObj(bo, SmallMiUserDiscount.class);
        //调用通用mapper
        List<SmallMiUserDiscount> select = userDiscountMapper.select(smallMiUserDiscount);
        //转换返回值
        List<GetUserDiscountByParamDO> getUserDiscountByParamDOS = BeanCopierUtil.copyList(select, GetUserDiscountByParamDO.class);
        return getUserDiscountByParamDOS;
    }


    /**
     * @return
     * @Author lyw
     * @Description 通过id删除优惠券
     * @Date 14:41 2021/7/21
     * @Param
     */
    @Override
    public RestResult delUserDiscountById(String discountId) {
        //调用通用mapper
        int i = userDiscountMapper.deleteByPrimaryKey(discountId);
        //判断返回值
        if (i == 0) {
            return new RestResult(ResultEnum.DEL_ERROR);
        }
        return new RestResult(ResultEnum.DEL_SUCCESS);
    }


    /**
     * @return
     * @Author lyw
     * @Description 修改用户优惠券
     * @Date 14:47 2021/7/21
     * @Param
     */
    @Override
    public RestResult editUserDiscount(EditUserDiscountBO bo) {
        //转换参数
        SmallMiUserDiscount smallMiUserDiscount = BeanCopierUtil.copyObj(bo, SmallMiUserDiscount.class);
        //调用通用mapper
        int i = userDiscountMapper.updateByPrimaryKeySelective(smallMiUserDiscount);
        //判断返回值
        if (i == 0) {
            return new RestResult(ResultEnum.EDIT_ERROR);
        }
        return new RestResult(ResultEnum.EDIT_SUCCESS);
    }


    /**
     * @return
     * @Author lyw
     * @Description 添加用户优惠券
     * @Date 14:51 2021/7/21
     * @Param
     */
    @Override
    public RestResult addUserDiscount(AddUserDiscountBO bo) {
        //转换参数
        SmallMiUserDiscount smallMiUserDiscount = BeanCopierUtil.copyObj(bo, SmallMiUserDiscount.class);
        //随机生成优惠券id
        ObjectIdGenerator objectIdGenerator = new ObjectIdGenerator();
        String next = objectIdGenerator.next();
        smallMiUserDiscount.setDiscountId(next);
        //调用通用mapper
        int i = userDiscountMapper.insertSelective(smallMiUserDiscount);
        //判断返回值
        if (i == 0) {
            return new RestResult(ResultEnum.ADD_ERROR);
        }
        return new RestResult(ResultEnum.ADD_SUCCESS);
    }


}
